package org.inspiration.dudu.user.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.inspiration.dudu.user.model.dto.UserLikeDTO;
import org.inspiration.dudu.user.model.entity.UserLikeEntity;

import java.util.List;

/**
 * 用户点赞数据访问层接口
 * 继承自 MyBatis-Plus 的 BaseMapper，提供基本的 CRUD 操作
 */
@Mapper
public interface UserLikeMapper extends BaseMapper<UserLikeEntity> {

    /**
     * 创建用户点赞记录
     * 
     * @param userLikeEntity 用户点赞实体
     */
    @Insert("insert into user_like (user_id, target_user_id,content_id, content_type) VALUES (#{userId}, #{targetUserId},#{contentId}, #{contentType})")
    void createUserLike(UserLikeEntity userLikeEntity);

    /**
     * 删除用户点赞记录
     * 
     * @param userLikeEntity 用户点赞实体
     */
    @Delete("delete from user_like where user_id = #{userId} and content_id = #{contentId} and content_type = #{contentType}")
    void deleteUserLike(UserLikeEntity userLikeEntity);

    /**
     * 检查用户是否已点赞
     * 
     * @param userId 用户ID
     * @param contentId 内容ID
     * @return 点赞数量
     */
    @Select("select count(*) from user_like where user_id = #{userId} and content_id = #{contentId}")
    int isLiked(Long userId, String contentId);



    /**
     * 将点赞标记为已读
     */
    @Update("update user_like set is_read = 1 where user_id = #{userId} and content_id = #{contentId} and content_type = #{contentType}")
    void markAsRead(UserLikeEntity userLikeEntity);

    /**
     * 获取用户最近的点赞记录
     * 
     * @param userId 用户ID
     * @param offset 偏移量
     * @param pageSize 页面大小
     * @return 用户最近的点赞记录列表
     */
    @Select("select * from user_like where user_id = #{userId} and content_type != 'comment' order by liked_at desc limit #{offset}, #{pageSize}")
    List<UserLikeEntity> getLikesOrderByDESC(Long userId, Integer offset, Integer pageSize);

    @Select("select is_read from user_like where user_id = #{userId} and content_id = #{contentId} and content_type = #{contentType}")
    Boolean getIsRead(UserLikeEntity userLikeEntity);

    @Select("select * from user_like where user_id = #{userId} and content_id = #{contentId} and content_type = #{contentType}")
    UserLikeEntity getUserLike(UserLikeDTO userLikeDTO);

    @Select("select * from user_like where target_user_id = #{userId} order by liked_at desc limit #{offset}, #{pageSize}")
    List<UserLikeEntity> getUserLikesByTargetUserIdOrderByDESC(Long userId, Integer offset, Integer pageSize);


}
